//
//  JXWeekdayIndicatorView.m
//  TestCollectionView
//
//  Created by Lemon on 2019/4/21.
//  Copyright © 2019 sunshinfight@163.com. All rights reserved.
//

#import "JXWeekdayIndicatorView.h"

@implementation JXWeekdayIndicatorView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _weekdayDescriptions = @[@"日", @"一", @"二", @"三",
                                 @"四", @"五", @"六"];
        [self configUI];
    }
    return self;
}

- (instancetype)init
{
    self = [[JXWeekdayIndicatorView alloc] initWithFrame:CGRectZero];
    return self;
}

- (void)setWeekdayDescriptions:(NSArray<NSString *> *)weekdayDescriptions
{
    NSAssert(weekdayDescriptions.count != 7, @"weekdayDescriptions.count must be 7.");
    _weekdayDescriptions = weekdayDescriptions;
}

- (void)configUI
{
    self.backgroundColor = UIColor.whiteColor;
    for (int i = 0; i < 7; i++) {
        UILabel *lab = [UILabel new];
        [self addSubview:lab];
        lab.tag = 10 + i;
        lab.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];
        lab.font = [UIFont systemFontOfSize:13];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.text = self.weekdayDescriptions[i];
    }
}

- (void)setInsets:(UIEdgeInsets)insets
{
    _insets = insets;
    [self setNeedsLayout];
}

- (void)setMinimumInteritemSpacing:(CGFloat)minimumInteritemSpacing
{
    _minimumInteritemSpacing = minimumInteritemSpacing;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    const CGFloat labW = (self.bounds.size.width - _insets.left - _insets.right - _minimumInteritemSpacing*6)/7;
    const CGFloat labH = self.bounds.size.height;
    for (int i = 0; i < 7; i++) {
        UILabel *lab = [self viewWithTag:i+10];
        if (lab) {
            CGFloat labX = labW*i + _insets.left + i * _minimumInteritemSpacing;
            lab.frame = CGRectMake(labX, 0, labW, labH);
        }
    }
}
@end

